import { Component, OnInit } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { Plan } from './plan';
import { PlanInforPage } from '../planInfor/planInfor';
import { PlanAddPage } from '../planAdd/planAdd';
import { HabitService } from '../../service/habit.service';

@Component({
  selector: 'page-plan',
  templateUrl: 'plan.html'
})
export class PlanPage implements OnInit{
  planList : Array<Plan>;
  constructor(public navCtrl: NavController, public habitService : HabitService, public alertCtrl: AlertController) {
    this.planList = [];

  }
  showAlert() {
    let alert = this.alertCtrl.create({
      subTitle: '删除成功！',
      buttons: ['确定']
    });
    alert.present();
  }

  enterPlanInforPage(i, flag : boolean){
    this.navCtrl.push(PlanInforPage,{
      plan : this.planList[i],
      isEdit : flag,
      isAdd : false
    });
  }

  enterPlanAddPage(){
    this.navCtrl.push(PlanAddPage);
  }

  ngOnInit() : void{
    this.getPlan();
  }
  getPlan(){
    this.habitService.getPlan().then(
      planList=>{
      this.planList=planList;
      this.habitService.planList = this.planList;
    }
    );
  }
  deletePlan(i){
    this.habitService.deletePlan(this.planList[i]).then(
      ()=>{
        this.getPlan();
        this.showAlert();
      }
    )
  }
}
